package com.ayq.activiti.manager.controller;

import com.ayq.activiti.manager.service.CustomModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.activiti.editor.constants.ModelDataJsonConstants;
import org.activiti.engine.ActivitiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;


@RestController
@RequestMapping("/service")
@Api(tags = "流程模型中心")
@Slf4j
public class ModelSaveRestResource implements ModelDataJsonConstants {
  
  protected static final Logger LOGGER = LoggerFactory.getLogger(ModelSaveRestResource.class);

  @Autowired
  private CustomModelService customModelService;

  /**
   * <p>保存流程模型</p>
   * <p>方法名:saveModel</p>
   * @Author ayq
   * @Date 2021/3/8 16:12
   * @Param [modelId, values]
   * @Return void
   */
  @ApiOperation(value = "保存流程模型", notes = "保存流程模型")
  @RequestMapping(value="/model/{modelId}/save", method = RequestMethod.PUT)
  @ResponseStatus(value = HttpStatus.OK)
  public void saveModel(@PathVariable String modelId, String name,String description,String json_xml,String svg_xml) {
    ByteArrayOutputStream outStream = null;
    try {
      customModelService.saveModel(modelId,name,description,json_xml,svg_xml,outStream);
    } catch (Exception e) {
      LOGGER.error("保存流程模型出错", e);
      throw new ActivitiException("保存流程模型出错", e);
    } finally {
      if (outStream != null) {
        try {
          outStream.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
}
